#include "codehighlighter.h"

//用json文件保存各种语言的关键字

QString CodeHighlighter::cppKeyword {"alignas|alignof|and|and_eq|asm|atomic_cancel|atomic_commit|atomic_noexcept|auto|bitand|bitor|bool|break|"
                                   "case|catch|char|char8_t|char16_t|char32_t|class|compl|concept|const|consteval|constexpr|constinit|const_cast|continue|co_await|"
                                   "co_return|co_yield|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|false|float|for|friend|goto|if|"
                                   "inline|int|long|mutable|namespace|new|noexcept|not|not_eq|nullptr|operator|or|or_eq|private|protected|public|reflexpr|register|"
                                   "reinterpret_cast|requires|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|synchronized|template|this|"
                                    "thread_local|throw|true|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while|xor|xor_eq"};

CodeHighlighter::CodeHighlighter(QTextDocument *parent, const Language language)
    : QSyntaxHighlighter(parent)
{
    QTextCharFormat sss;
    sss.setForeground(QBrush(QColor(59, 59, 59)));  // 设置括号颜色为黄色
    addHighlightingRule(".", sss);  // 任意字符

    QString key;

    switch (language) {
    case Language::Cpp:
        key = cppKeyword;
        break;
    case Language::C:
    case Language::Java:
    case Language::Go:
        key = "func|interface|select|case|defer|go|map|struct|chan|else|goto";
    case Language::Python:
    case Language::Text:
        break;
    }

    // 定义语法格式
    QTextCharFormat keywordFormat;
    keywordFormat.setForeground(QBrush(QColor(0, 0, 255)));  // 设置关键词颜色为蓝色
    keywordFormat.setFontWeight(QFont::Bold);  // 设置加粗
    addHighlightingRule("\\b(" + key + ")\\b", keywordFormat);  // 关键词

    QTextCharFormat numberFormat;
    numberFormat.setForeground(QBrush(QColor(9, 134, 88)));  // 设置标识符颜色为绿色
    addHighlightingRule("-?\\d+", numberFormat);  // 匹配任意数字

    QTextCharFormat functionFormat;
    functionFormat.setForeground(QBrush(QColor(121, 94, 38)));  // 设置标识符颜色为绿色
    addHighlightingRule("\\b\\w+\\s*\\(\\)", functionFormat);  // 匹配函数

    QTextCharFormat bracketFormat;
    bracketFormat.setForeground(QBrush(QColor(4, 49, 250)));  // 设置括号颜色为黄色
    addHighlightingRule("\\{|\\}|\\[|\\]|\\(|\\)", bracketFormat);  // 括号
}
/*
    // 定义语法格式
    QTextCharFormat keywordFormat;
    keywordFormat.setForeground(QBrush(QColor(0, 0, 255)));  // 设置关键词颜色为蓝色
    keywordFormat.setFontWeight(QFont::Bold);  // 设置加粗
    addHighlightingRule("\\b(" + cppKeyword + ")\\b", keywordFormat);  // 关键词
*/

void CodeHighlighter::highlightBlock(const QString &text)
{
    for (const HighlightingRule &rule : highlightingRules) {
        QRegularExpression expression(rule.pattern);
        QRegularExpressionMatchIterator matchIterator = expression.globalMatch(text);
        while (matchIterator.hasNext()) {
            QRegularExpressionMatch match = matchIterator.next();
            setFormat(match.capturedStart(), match.capturedLength(), rule.format);
        }
    }
}

void CodeHighlighter::addHighlightingRule(const QString &pattern, const QTextCharFormat &format)
{
    HighlightingRule rule;
    rule.pattern = pattern;
    rule.format = format;
    highlightingRules.append(rule);
}
